package com.my.mvc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.my.mvc.actionsupport.MyActionSupport;
import com.my.mvc.baseinterface.ActionInterface;
import com.my.mvc.fullbean.FullBean;
import com.my.mvc.mapping.ActionMapping;

public class ActionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		MyActionSupport myActionSupport = FullBean.fullBean(request);
		ActionInterface actionInterface = null;
		Map<String, String> map = ActionMapping.getMap();
		String actionName = map.get(request.getParameter("sign"));
		try {
			Class<?> clazz = Class.forName(actionName);
			actionInterface = (ActionInterface) clazz.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String message = actionInterface.execute(myActionSupport);
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
